package com.transferwallet.sdk;

import java.io.File;


import com.transferwallet.sdk.entity.BusinessResidentReq;
import com.transferwallet.sdk.entity.CashOutForSelfReq;
import com.transferwallet.sdk.entity.ConfirmPayReq;
import com.transferwallet.sdk.entity.DrawbackReq;
import com.transferwallet.sdk.entity.LoadWithRepayPlanReq;
import com.transferwallet.sdk.entity.OrderChangeStatusReq;
import com.transferwallet.sdk.entity.OrderSubmissionReq;
import com.transferwallet.sdk.entity.QueryAccountingDetailsReq;
import com.transferwallet.sdk.entity.QueryBatchDetailsReq;
import com.transferwallet.sdk.entity.QueryBatchEssentialsReq;
import com.transferwallet.sdk.entity.QueryOpenAccountReq;
import com.transferwallet.sdk.entity.QueryVritualAccountsReq;
import com.transferwallet.sdk.entity.SingleTransferReq;
import com.transferwallet.sdk.service.TransferWalletService;
import lombok.extern.slf4j.Slf4j;

/**
 * Title: TransferWalletClient.java Description: SDK客户端
 * 
 * @author llj
 * @date 2019年8月7日
 */
@Slf4j
public class TransferWalletClient {


	/**
	 * 商户编码
	 */
	private String memberCode;
	/**
	 * 私钥
	 */
	private String privateKey;
	/**
	 * 服务器host
	 */
	private String serviceHost;

	private TransferWalletService transferWalletService = new TransferWalletService();

	/**
	 * 推荐构造方法
	 * 
	 * @param memberCode
	 * @param privateKey
	 * @param serviceHost
	 */
	public TransferWalletClient(String memberCode, String privateKey, String serviceHost) {
		this.memberCode = memberCode;
		this.privateKey = privateKey;
		this.serviceHost = serviceHost;
	}

	public String getMemberCode() {
		return memberCode;
	}

	public void setMemberCode(String memberCode) {
		this.memberCode = memberCode;
	}

	public String getPrivateKey() {
		return privateKey;
	}

	public void setPrivateKey(String privateKey) {
		this.privateKey = privateKey;
	}

	public String getServiceHost() {
		return serviceHost;
	}

	public void setServiceHost(String serviceHost) {
		this.serviceHost = serviceHost;
	}

	/**
	 * 2.1 商户入网
	 * 
	 * @param batchNo
	 * @param filePath
	 * @return
	 */
	public String businessResident(String batchNo, String filePath) {

		log.debug("商户入网开始...");
		BusinessResidentReq businessResidentReq = new BusinessResidentReq();
		businessResidentReq.setMemCode(memberCode);
		businessResidentReq.setBatchNo(batchNo);
		String result = transferWalletService.businessResident(memberCode, privateKey, serviceHost, businessResidentReq,
				filePath);
		log.debug("商户入网结果：" + result);
		return result;

	}

	/**
	 * 2.2 资金划转
	 * 
	 * @param batchNo
	 * @param transferFilePath  文件路径
	 * @param fundAccountType
	 * @param fundAccountType
	 * @return
	 */
	public String batchTransfer(String batchNo, String fundAccountNo, String fundAccountType, String transferFilePath) {
		log.info("资金划转开始...");
		LoadWithRepayPlanReq loadWithRepayPlanReq = new LoadWithRepayPlanReq();
		loadWithRepayPlanReq.setMemCode(memberCode);
		loadWithRepayPlanReq.setBatchNo(batchNo);
		loadWithRepayPlanReq.setFundAccountNo(fundAccountNo);
		loadWithRepayPlanReq.setFundAccountType(fundAccountType);
		String result = transferWalletService.batchTransfer(memberCode, privateKey, serviceHost, loadWithRepayPlanReq,
				transferFilePath);
		log.info("资金划转结果：" + result);
		return result;
	}

	/**
	 * 2.2 资金划转
	 * 
	 * @param batchNo
	 * @param transferFile
	 * @return
	 */
	public String batchTransfer(String batchNo, File transferFile) {
		log.info("资金划转开始...");
		LoadWithRepayPlanReq loadWithRepayPlanReq = new LoadWithRepayPlanReq();
		loadWithRepayPlanReq.setMemCode(memberCode);
		loadWithRepayPlanReq.setBatchNo(batchNo);
		String result = transferWalletService.batchTransfer(memberCode, privateKey, serviceHost, loadWithRepayPlanReq,
				transferFile);
		log.info("资金划转结果：" + result);
		return result;
	}

	/**
	 * 2.3 单笔提现
	 * 
	 * @param orderNo
	 * @param vritualMemCode
	 * @param occurAmount
	 * @param remark
	 * @return
	 */
	public String cashOutForSelf(String orderNo, String vritualMemCode, Long occurAmount, String remark) {
		log.info("单笔提现");
		CashOutForSelfReq cashOutForSelfReq = new CashOutForSelfReq();
		cashOutForSelfReq.setMemCode(memberCode);
		cashOutForSelfReq.setOrderNo(orderNo);
		cashOutForSelfReq.setVritualMemCode(vritualMemCode);
		cashOutForSelfReq.setOccurAmount(occurAmount);
		cashOutForSelfReq.setRemark(remark);
		String result = transferWalletService.cashOutForSelf(memberCode, privateKey, serviceHost, cashOutForSelfReq);

		log.info("单笔提现结果：" + result);
		return result;
	}

	/**
	 * 2.5 查询开户结果
	 * 
	 * @param currentPage
	 * @param pageSize
	 * @param memFullName
	 * @param idNumber
	 * @param mobile
	 * @param bankAccount
	 * @return
	 */
	public String queryOpenAccount(int currentPage, int pageSize, String memFullName, String idNumber, String mobile,
			String bankAccount) {
		log.info("查询开户结果");
		QueryOpenAccountReq queryOpenAccountReq = new QueryOpenAccountReq();
		queryOpenAccountReq.setMemCode(memberCode);
		queryOpenAccountReq.setCurrentPage(currentPage);
		queryOpenAccountReq.setPageSize(pageSize);
		queryOpenAccountReq.setMemFullName(memFullName);
		queryOpenAccountReq.setIdNumber(idNumber);
		queryOpenAccountReq.setMobile(mobile);
		queryOpenAccountReq.setBankAccount(bankAccount);

		String result = transferWalletService.queryOpenAccount(memberCode, privateKey, serviceHost,
				queryOpenAccountReq);
		log.info("查询开户结果结果：" + result);
		return result;
	}

	/**
	 * 2.6 按批次号查询批次概要信息
	 * 
	 * @param batchNo
	 * @return
	 */
	public String queryBatchEssentials(String batchNo) {
		log.info("查询批次概要信息");

		QueryBatchEssentialsReq queryBatchEssentialsReq = new QueryBatchEssentialsReq();
		queryBatchEssentialsReq.setMemCode(memberCode);
		queryBatchEssentialsReq.setBatchNo(batchNo);

		String result = transferWalletService.queryBatchEssentials(memberCode, privateKey, serviceHost,
				queryBatchEssentialsReq);
		log.info("查询批次概要信息结果：" + result);
		return result;
	}

	/**
	 * 2.7按批次号查询批次明细
	 * 
	 * @param batchNo
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	public String queryBatchDetails(String batchNo, int currentPage, int pageSize) {
		log.info("按批次号查询批次明细");

		QueryBatchDetailsReq queryBatchDetailsReq = new QueryBatchDetailsReq();
		queryBatchDetailsReq.setMemCode(memberCode);
		queryBatchDetailsReq.setBatchNo(batchNo);
		queryBatchDetailsReq.setCurrentPage(currentPage);
		queryBatchDetailsReq.setPageSize(pageSize);

		String result = transferWalletService.queryBatchDetails(memberCode, privateKey, serviceHost,
				queryBatchDetailsReq);

		log.info("按批次号查询批次明细结果：" + result);
		return result;
	}

	/**
	 * 2.8 查询账户明细
	 * 
	 * @param currentPage
	 * @param pageSize
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public String queryAccountingDetails(int currentPage, int pageSize, String startTime, String endTime) {
		log.info("查询账户明细");

		QueryAccountingDetailsReq queryAccountingDetailsReq = new QueryAccountingDetailsReq();
		queryAccountingDetailsReq.setCurrentPage(currentPage);
		queryAccountingDetailsReq.setPageSize(pageSize);
		queryAccountingDetailsReq.setMemCode(memberCode);
		queryAccountingDetailsReq.setStartTime(startTime);
		queryAccountingDetailsReq.setEndTime(endTime);

		String result = transferWalletService.queryAccountingDetails(memberCode, privateKey, serviceHost,
				queryAccountingDetailsReq);
		log.info("查询账户明细结果：" + result);
		return result;
	}

	/**
	 * 2.9 资金账户余额查询
	 * 
	 * @param currentPage
	 * @param pageSize
	 * @param vritualMemCode
	 * @return
	 */
	public String queryVritualAccounts(int currentPage, int pageSize, String vritualMemCode) {
		log.info("查询虚户余额");

		QueryVritualAccountsReq queryVritualAccountsReq = new QueryVritualAccountsReq();

		queryVritualAccountsReq.setMemCode(memberCode);
		queryVritualAccountsReq.setVritualMemCode(vritualMemCode);
		queryVritualAccountsReq.setCurrentPage(currentPage);
		queryVritualAccountsReq.setPageSize(pageSize);

		String result = transferWalletService.queryVritualAccounts(memberCode, privateKey, serviceHost,
				queryVritualAccountsReq);
		log.info("查询虚户余额结果：" + result);
		return result;
	}

	/**
	 * 4.1 订单上报
	 * 
	 * @param orderSubmissionReq
	 * @return
	 */
	public String orderSubmission(OrderSubmissionReq orderSubmissionReq) {
		log.info(" 4.1 订单上报");
		orderSubmissionReq.setMemCode(memberCode);

		String result = transferWalletService.orderSubmission(memberCode, privateKey, serviceHost, orderSubmissionReq);
		log.info(" 4.1 订单上报结果：" + result);

		return result;
	}

	/**
	 * 4.2 订单状态修改
	 * 
	 * @param orderChangeStatusReq
	 * @return
	 */
	public String orderChangeStatus(OrderChangeStatusReq orderChangeStatusReq) {
		log.info("4.2 订单状态修改");
		orderChangeStatusReq.setMemCode(memberCode);

		String result = transferWalletService.orderChangeStatus(memberCode, privateKey, serviceHost,
				orderChangeStatusReq);
		log.info("4.2 订单状态修改结果：" + result);

		return result;

	}

	/**
	 * 4.3 支付确认信息
	 * 
	 * @param confirmPayReq
	 * @return
	 */
	public String confirmPay(ConfirmPayReq confirmPayReq) {
		log.info("4.3 支付确认信息");
		confirmPayReq.setMemCode(memberCode);
		String result = transferWalletService.confirmPay(memberCode, privateKey, serviceHost, confirmPayReq);
		log.info("4.3 支付确认信息结果：" + result);

		return result;

	}

	/**
	 * 4.4 支付退款信息
	 * 
	 * @param drawbackReq
	 * @return
	 */
	public String drawback(DrawbackReq drawbackReq) {
		log.info("4.4 支付退款信息");
		drawbackReq.setMemCode(memberCode);

		String result = transferWalletService.drawback(memberCode, privateKey, serviceHost, drawbackReq);

		log.info("4.4 支付退款信息结果：" + result);
		return result;
	}

	/**
	 * 2.10 单笔资金账户划转
	 * 
	 * @param fromFundAccountNo
	 * @param toFundAccountNo
	 * @param occurAmount
	 * @param orderNo
	 * @return
	 */
	public String singleTransfer(String fromFundAccountNo, String toFundAccountNo, String occurAmount, String orderNo) {

		log.info("2.10 单笔资金账户划转");

		SingleTransferReq singleTransferReq = new SingleTransferReq();
		singleTransferReq.setMemCode(memberCode);
		singleTransferReq.setFromFundAccountNo(fromFundAccountNo);
		singleTransferReq.setToFundAccountNo(toFundAccountNo);
		singleTransferReq.setOccurAmount(occurAmount);
		singleTransferReq.setOrderNo(orderNo);

		String result = transferWalletService.singleTransfer(memberCode, privateKey, serviceHost, singleTransferReq);

		log.info("2.10 单笔资金账户划转结果：" + result);

		return result;
	}

}
